package com.banciyuan.bcywebview.base.view.listview;

import android.view.View;
import android.widget.AbsListView;

import com.banciyuan.bcywebview.base.view.recycleview.OnRecScrollListener;

/**
 * Created by squall on 2016/8/16.
 */
public class OnLvResultScrollListener implements OnRecScrollListener, AbsListView.OnScrollListener {

    private int lastY = -1;
    private int lastVisibleItem;
    private boolean mLastItemVisible;

    @Override
    public void onBottom() {

    }

    @Override
    public void onTop() {

    }

    @Override
    public void onBottom(int FirstVisibleItemPosition) {

    }

    @Override
    public void up() {

    }

    @Override
    public void down() {

    }

    @Override
    public void firstShow() {

    }

    @Override
    public void move() {

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
            if (mLastItemVisible) {
                onBottom();
            } else {
                onTop();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        mLastItemVisible = (totalItemCount > 0) && (firstVisibleItem + visibleItemCount >= totalItemCount - 1);

        View firstVisibleView = view.getChildAt(0);
        if (null == firstVisibleView) return;
        int nowY = Math.abs(firstVisibleView.getTop());
        int compare = nowY - lastY;
        if (firstVisibleItem == 0) {
            if (-1 == lastY) {
                lastY = nowY;
            }
            firstShow();
            lastY = nowY;
            lastVisibleItem = firstVisibleItem;
            return;
        }

        if (lastVisibleItem != firstVisibleItem) {
            lastY = -1;
            lastVisibleItem = firstVisibleItem;
            return;
        }

        if (-1 == lastY) {
            lastY = nowY;
            lastVisibleItem = firstVisibleItem;
            return;
        }
        if (compare < -5) {
            up();
        } else if (compare > 5) {
            down();
        }
        lastY = nowY;
        lastVisibleItem = firstVisibleItem;
    }
}
